#
# pluginHelper.py - helper functions for
# plugins.
#

#
# plugin data structure:
class pluginData:
	name = ""
	type = None
	description = ""
	options = {}

	scan_fn = None
	scan_fn_type = None
	results = []
	showSrc = True
	
#
# Initialise the above data structure:
def init():
	#out("Initialising plugin data structures for re-use")
	pluginData.name = ""
	pluginData.type = None
	pluginData.description = ""
	pluginData.options.clear()
	pluginData.scan_fn = None
	pluginData.scan_fn_type = None
	del pluginData.results[:]
	pluginData.showSrc = True

#
# describe a plugin:
def describePlugin(name, type, description, showSrc=True):
	pluginData.name = name
	pluginData.type = type
	pluginData.description = description
	pluginData.showSrc = showSrc

def addOption(name, description, default):
	pluginData.options[name] = (description,default)

def setScanFunction(fnType, fObj):
	pluginData.scan_fn = classmethod(fObj)
	pluginData.scan_fn_type = fnType

def getOption(name):
	description,value = pluginData.options.get(name,(None,None))

	return value

#
# report an error. There are several basic forms:
# reportError(lineNumber, severity) -> reports a simple error by line number alone.
# reportError(lineNumber, col, severity) -> reports an error by line and column position.
# reportError(lineNumber, col, lineCount, severity) -> Reports an error at the line and col givem, that spens more than one line.
def reportError(*args):
	if len(args) < 2:
		raise "invalid number of arguments to reportError"

	out("REPORTING ERROR", args)

	line = args[0]
	severity = args[-1]
	col = 0
	if len(args) > 2:
		col = args[1]
	lineCount = 1
	if len(args) > 3:
		lineCount = args[2]

	r = (line,col, lineCount, severity)
	pluginData.results.append(r)

# scanner types:
SCANNER_TYPE_LINE_BY_LINE = 1 << 0
SCANNER_TYPE_FILE_OBJECT = 1 << 1
SCANNER_TYPE_FILE_PATH = 1 << 2

#init()

# todo - we can probably remove this now.
def test():
	for item in dir(pluginData):
		try:
			s = eval("pluginData." + item)
		except NameError:
			continue
		print "%s:%s" % (item,s)


#
# plugins can use this to print stuff, since they wouldn't normally be allowed:

from pprint import pprint
import sys
def out(*args):
	pprint(args, sys.stderr)


